﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Xrm.Framework.Test.Integration;
using Xrm.Solutions.FrameworkSample.Common.Entities;
using Microsoft.Crm.Sdk.Messages;


namespace Xrm.Solutions.FrameworkSample.XrmIntegrationTests.WFActivities
{
    [TestClass]
    public class TestTotalOpportunitiesActivity : XrmIntegrationTest
    {
        #region Instance Variables

        private Account account;
        private List<Opportunity> accountOpportunities;

        #endregion

        #region Setup

        protected override OrganizationRequest SetupTriggerRequest()
        {
            CrmHelpers.SetConfigurationSetting(this.OrganizationService,"AutoNumberPrefix","Test"+DateTime.Now);

            account = new Account();
            account.Name = "TestTotalOpportunitiesActivity " + DateTime.Now;
            account.Id = this.OrganizationService.Create(account);

            Opportunity opportunity1 = new Opportunity();
            opportunity1.Name = "TestTotalOpportunitiesActivity 1 " + DateTime.Now;
            opportunity1.ActualValue = new Money(1000);
            opportunity1.ParentAccountId = account.ToEntityReference();
            Opportunity opportunity2 = new Opportunity();
            opportunity2.Name = "TestTotalOpportunitiesActivity 2 " + DateTime.Now;
            opportunity2.ActualValue = new Money(3000);
            opportunity2.ParentAccountId = account.ToEntityReference();

            opportunity1.Id = this.OrganizationService.Create(opportunity1);
            opportunity2.Id = this.OrganizationService.Create(opportunity2);

            accountOpportunities = new List<Opportunity>(2);
            accountOpportunities.Add(opportunity1);
            accountOpportunities.Add(opportunity2);

            OrganizationRequest executeActionRequest = new OrganizationRequest("xdft_TestTotalOpportunitiesActivity");
            executeActionRequest["Target"] = account.ToEntityReference();
            return executeActionRequest;
        }

        #endregion

        #region Test

        [TestMethod]
        public void RunTestTotalOpportunitiesActivity()
        {
            base.Test();
        }

        #endregion

        #region Verify

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Assert.IsTrue(this.TriggerResponse.Results.Contains("AccountValue"));
            
            decimal expectedTotal = this.accountOpportunities.Sum(op => op.ActualValue.Value);

            decimal actualTotal = ((Money)this.TriggerResponse["AccountValue"]).Value;

            Assert.AreEqual(expectedTotal, actualTotal);
        }

        #endregion

        #region Clean up

        protected override void CleanUp()
        {
            base.CleanUp();

            foreach (Opportunity opp in accountOpportunities)
                this.OrganizationService.Delete(opp.LogicalName, opp.Id);

            this.OrganizationService.Delete(account.LogicalName, account.Id);

        }

        #endregion
    }
}
